package com.linewell.export.util;

import org.quartz.impl.triggers.CronTriggerImpl;

import java.util.Date;

/**
 * @Title: QuTest
 * @description:
 * @author: pzhongqaing@linewell.com
 * @since: 2020年10月16日 10:19
 */
public class QuartzTest {
     /*public static void main(String[] args) {
        try {
            //1.创建Scheduler的工厂
            SchedulerFactory sf = new StdSchedulerFactory();

            //2.从工厂中获取调度器实例
            Scheduler scheduler = sf.getScheduler();


            //3.创建JobDetail(作业信息)
            JobDetail jb = JobBuilder.newJob(TaskJob.class)
                    .withDescription("this is a test job") //job的描述
                    .withIdentity("testJob", "testGroup") //job 的name和group
                    .build();


            //向任务传递数据
            JobDataMap jobDataMap = jb.getJobDataMap();
            jobDataMap.put("uname", "张三");

            //任务运行的时间，SimpleSchedle类型触发器有效
            long time = System.currentTimeMillis() + 3 * 1000L; //3秒后启动任务
            Date statTime = new Date(time);

            //4.创建Trigger
            //使用SimpleScheduleBuilder或者CronScheduleBuilder
            Trigger t = TriggerBuilder.newTrigger()
                    .withDescription("")
                    .withIdentity("ramTrigger", "ramTriggerGroup")
                    .startAt(statTime)  //默认当前时间启动
                    //普通计时器
                    //.withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(3).withRepeatCount(3))//间隔3秒，重复3次
                    //表达式计时器
                    .withSchedule(CronScheduleBuilder.cronSchedule("0/3 * * * * ?")) //3秒执行一次
                    .build();

            //5.注册任务和定时器
            scheduler.scheduleJob(jb, t);

            //6.启动 调度器
            scheduler.start();
        }catch (Exception e) {
            e.printStackTrace();
        }

    }*/


    /**
     * 手动创建线程池
     */
//    private static ThreadPoolExecutor executor = new  ThreadPoolExecutor(20,25,100L,
//            TimeUnit.SECONDS,new LinkedBlockingQueue<>(),new ThreadPoolExecutor.CallerRunsPolicy());

//    public static void main(String[] args) {
//        Student student = null;
//        List<Student> students = new ArrayList<>();
//        //添加五十万个学生元素
//        for (int i = 0; i < 500000; i++) {
//            student = new Student("name"+i,20,183,"玩");
//            students.add(student);
//        }
//        CountDownLatch countDownLatch = new CountDownLatch(students.size());
//        for (Student student1 : students) {
//            /**
//             * 给元素添加后缀
//             */
//            executor.submit(()-> {
//                try {
//                    student1.setName(student1.getName()+"这是后缀");
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }finally {
//                    countDownLatch.countDown();
//                }
//            });
//        }
//            /*try {
//                countDownLatch.await();
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }*/
//        //查看添加情况
//        System.out.println("添加数量："+students.stream().filter(x->x.getName().contains("这是后缀")).count());
//    }
    public static void main(String[] args) {
        System.out.println(isValidExpression("0 */5 * * * ?"));
    }

    /**
     * 判断cron时间表达式正确性
     *
     * @param cronExpression 字符串
     * @return boolean
     */
    private static boolean isValidExpression(String cronExpression) {
        CronTriggerImpl trigger = new CronTriggerImpl();
        try {
            trigger.setCronExpression(cronExpression);
            Date date = trigger.computeFirstFireTime(null);
            return date != null && date.after(new Date());
        } catch (Exception e) {
//            log.error(e.getMessage(), e);
        }
        return false;
    }
}
